﻿/**
 * ...
 * @author wangjun
 */
import mx.utils.Delegate
import org.casalib.util.ArrayUtil;
class cn.eminia.A2.utils.uMC 
{
	
	public function uMC() 
	{
		
	}
	/**
		随机等待.
		
		@param scope: 指定MC.
		@param range: 范围.
		@param partNum: 段数.
		@return A new Array comprised of passed Array's items in a random order.
		@usage
			<code>
				var numberArray:Array = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
				trace(ArrayUtil.randomize(numberArray));
			</code>
		@since Flash Player 7
	*/
	public static function randomWait(scope:MovieClip, range:Array, partNum:Number) {
		if (range.length < 2) throw new Error("!!!--Error-- uMC::randomWait = 至少要2个元素")
		var max:Number;
		var min:Number;
		if (range[0] > range[1]) 
		{
			max = range[0];
			min = range[1];			
		}
		else 
		{
			max = range[1];
			min = range[0];			
		}
		if (max != null && min != null) {
			
			if (partNum == null||partNum == undefined) partNum = 5;
			var step:Number = (max - min) / partNum; 
			var second:Number = Math.floor((step * random(partNum)+min)*1000); 
			scope.stop();
			var obj = new Object();
			obj.doSth =function() {
				//trace("["+getTimer()+"]"+"--EXE-- className::ClueID")
				clearInterval(scope.__iTmpID__);
				scope.play();	 
				scope.randomWaitQuitCallBack();
			}
				//trace("["+getTimer()+"]"+"--EXE-- className::setInterval second="+second)
			scope.__iTmpID__ = setInterval(obj,'doSth', second);
			scope.randomWaitQuit = obj.doSth;
			
		}
	}
	public static function newMC(_ctnr:MovieClip,scope:MovieClip,onFrame:Function) {
		var ctnr = (_ctnr == null) ? _root : _ctnr;
		var dd = ctnr.getNextHighestDepth();
		var mc = ctnr.createEmptyMovieClip(dd.toString(), dd);
		
		if (onFrame != null && scope!=null) {
			// mc.onEnterFrame = Delegate.Function() {
				 
			//}
			 
			
			 mc.onEnterFrame = Delegate.create(scope, onFrame)
		}
		
		return  mc;
		 
	} 
	public static function playBack(mc:MovieClip):Void {
		mc.onEnterFrame=function(){
			this.gotoAndStop(this._currentframe-1);
			//trace('this._currentframe'+this._currentframe);
			if (this._currentframe == 1) {
				delete this.onEnterFrame;
			} 
		}
	}
	
	public static function delCtrl(mc) {
			delete  mc.onEnterFrame;
			mc.onEnterFrame = null;
			removeMovieClip (mc);
			
	}
	
	public static function orderBy(type, scope):Array {
		var arr:Array = [];
		if (type == '|') {
			for (var i in scope) {
				//if (scope[i] instanceof MovieClip) {
				if (scope[i].constructor == MovieClip) {
					arr.push({mc:scope[i], pos:Math.floor(scope[i]._y)});
				}
			}
		} else {
			for (var i in scope) {
				if (scope[i] instanceof MovieClip) {
					arr.push({mc:scope[i], pos:Math.floor(scope[i]._x)});
				}
			}
		}
		arr.sortOn('pos', Array.NUMERIC);
		//arr.reverse();
		//
		var arr2 = [];
		for (var i = 0; i<arr.length; i++) {
			//trace(arr[i].mc._name+' - '+arr[i].pos)
			arr2.push(arr[i].mc);
		}
		return arr2;
	}
}